
import {
  fleetMonitorApi,
  fleetMonitorByStatusApi,
  eventListApi,
  fleetMonitorAllApi,
  saveFtmMonitorApi,
  fleetMonitorConfigByUserApi
} from '@/services/fraMonitoringSetting.js';
import { message } from 'ant-design-vue';

export default {
  namespaced: true,
  state: {
    fleetMonitorLoading: false,
    fleetMonitorData: [],
    fleetMonitorByStatusLoading: false,
    eventListData: [],
    eventListLoading: false,
    allAcIndex: 0,
    configId: '',
    configData: {}
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  getters: {
    fleetMonitorDataF(state) {
      return state.fleetMonitorData?.[state.allAcIndex] ?? {};
    }
  },
  actions: {
    // FDE类型获取
    async fleetEventList({ commit }, params) {
      commit('setState', { key: 'eventListLoading', payload: true });
      const rep = await eventListApi(params);
      commit('setState', { key: 'eventListData', payload: rep.data });
      commit('setState', { key: 'eventListLoading', payload: false });
    },
    async saveFtmMonitor({ commit }, params) {
      commit('setState', { key: 'fleetMonitorLoading', payload: true });
      const rep = await saveFtmMonitorApi(params);
      if (rep.code === 200) {
        commit('setState', { key: 'configId', payload: rep.data });
        message.success(rep.message);
        commit('setState', { key: 'fleetMonitorLoading', payload: false });
        return Promise.resolve(true);
      } else {
        message.error(rep.message);
      }
      commit('setState', { key: 'fleetMonitorLoading', payload: false });
    },
    // 事件监控
    async fleetMonitor({ state, commit }) {
      commit('setState', { key: 'fleetMonitorLoading', payload: true });
      const rep = await fleetMonitorApi({ confId: state.configId });
      commit('setState', { key: 'fleetMonitorData', payload: rep.data });
      commit('setState', { key: 'fleetMonitorLoading', payload: false });
    },
    async fleetMonitorAll({ commit }, params) {
      commit('setState', { key: 'fleetMonitorLoading', payload: true });
      const rep = await fleetMonitorAllApi(params);
      commit('setState', { key: 'fleetMonitorData', payload: rep.data });
      commit('setState', { key: 'fleetMonitorLoading', payload: false });
    },
    // 事件监控
    async fleetMonitorConfigByUser({ state, commit, dispatch }) {
      const userinfo = JSON.parse(localStorage.getItem(process.env.VUE_APP_USERINFO));
      // commit('setState', { key: 'fleetMonitorLoading', payload: true });
      const rep = await fleetMonitorConfigByUserApi({ userId: userinfo.userId });
      if (rep.data) {
        commit('setState', { key: 'configId', payload: rep.data.id });
        commit('setState', { key: 'configData', payload: rep.data });
        dispatch('fleetMonitor');
      } else {
        dispatch('fleetMonitorAll');
      }

      // commit('setState', { key: 'fleetMonitorLoading', payload: false });
    },
    // 事件清除
    async fleetMonitorByStatus({ state, commit }) {
      if (!state.fleetMonitorData[0]?.eventId) return;
      commit('setState', { key: 'fleetMonitorByStatusLoading', payload: true });
      try {
        const rep = await fleetMonitorByStatusApi({ eventId: state.fleetMonitorData[0].eventId });
        if (rep.data === '成功') {
          commit('setState', { key: 'fleetMonitorData', payload: [] });
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'fleetMonitorByStatusLoading', payload: false });
      }
    }
  }
};
